package com.yu.footballpromotionliveadmin.dao.impl;

import com.yu.footballpromotionliveadmin.dao.ActivityTypeDao;
import com.yu.footballpromotionliveadmin.mapper.ActivityTypeMapper;
import com.yu.footballpromotionliveadmin.page.PageInfo;
import com.yu.footballpromotionliveadmin.pojo.ActivityType;
import com.yu.footballpromotionliveadmin.util.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Repository
public class ActivityTypeDaoImpl implements ActivityTypeDao {

    @Resource
    ActivityTypeMapper activityTypeMapper;

    @Override
    public List<ActivityType> getAllActivityTypes() throws Exception {
        return activityTypeMapper.selectByExample(null);
    }

    @Override
    public ActivityType getActivityTypeByUno(String actno) throws Exception {
        return activityTypeMapper.selectByPrimaryKey(actno);
    }

    @Override
    public List<ActivityType> getActivityTypesByPage(PageInfo pageInfo) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount =(int) activityTypeMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return activityTypeMapper.getActivityTypeByPage(map);
    }

    @Override
    public Integer deleteActivityType(String actno) throws Exception {
        return activityTypeMapper.deleteByKey(actno);
    }

    @Override
    public Integer updateActivityType(ActivityType activityType) throws Exception {

        return activityTypeMapper.updateByPrimaryKey(activityType);
    }

    @Override
    public Integer addActivityTypeByUno(ActivityType activityType) throws Exception {
        activityType.setActno(initId());
        return activityTypeMapper.insert(activityType);
    }

    @Override
    public ActivityType getActivityTypesWithOrders(String actno) throws Exception {
        return null;
    }

    @Override
    public ActivityType getAccountByAccountName(String actno) throws Exception {
        return null;
    }

    public String initId() {
        String maxUno = activityTypeMapper.getMaxUno();
        maxUno =  Optional.ofNullable(maxUno).orElse("A0000");
        return GenNumTools.initId("A", 4, maxUno);
    }
}
